<script setup lang="ts">
import { nextTick, reactive, ref } from "vue";
import { guild_roomApi } from "@/api/controller";
import { oss_url } from "@/pinia/getter";
import xmUpload from "@/components/Upload/xm-upload.vue";
import xmImage from "@/components/xmImage/index.vue";
import { ToastSuccess, Toastloading } from "@/utils/public";
import { ElMessageBox } from "element-plus";
const formLabelWidth = "120px";
const dataForm = ref();
const action = reactive({
  dialogTableVisible: false,
  dialogFormVisible: false,
  disabled: false,
  list: [] as Array<any>,
  title:''
});
interface formQuert {
  id:number
  name:string
  animate_resource:string
  new_animate_resource:string
}
let form = reactive({} as formQuert);
const formRules = {
  name: [{ required: true, trigger: "blur", message: "请填写标签名称" }],
  animate_resource: [{ required: true, trigger: "blur", message: "请上传标签图片" }],

};
const handleSubmit = () => {
  dataForm.value.validate(async (valid: any) => {
    if (!valid) return;
    let submitSet = false
    const loading = Toastloading()
    if(form.id) {
      await guild_roomApi.updateLabel(form).then(() => {
        submitSet = true
        ToastSuccess('修改成功')
      }).catch(() => {
        loading.close()
      });
    }
    if(!form.id) {
      await guild_roomApi.createLabel(form).then(() => {
        submitSet = true
        ToastSuccess('新增成功')
      }).catch(() => {
        loading.close()
      });
    }
    if(submitSet) {
      loading.close()
      getList()
      closeDialog()
    }
  });
};
const openForm = (row:any = {})=>{
  const { id=0, name='', animate_resource='', new_animate_resource='' } = row
  if(id) {
    action.title = '修改标签'
    Object.assign(form,{
      id,
      name,
      animate_resource,
      new_animate_resource
    })
  }else{
    action.title = '新增标签'
  }
  action.dialogFormVisible = true;
}
const handleDelete = (row:any = {})=>{
  ElMessageBox.confirm("确定删除此标签吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { id=0 } = row
    const loading = Toastloading();
    guild_roomApi.delLabel(id).then(()=>{
      ToastSuccess("删除成功!");
      loading.close();
      getList();
    }).catch(()=>{
      loading.close();
    })
  }).catch(() => {});
}
const getList = async()=>{
  const loading = Toastloading()
  const res = await guild_roomApi.getLabel()
  loading.close()
  const { data=[] } = res
  action.list = data
}
const openDialog = async(query: any = {}) => {
  await getList()
  action.dialogTableVisible = true;
};
const closeDialog = () => {
  form = reactive({} as formQuert);
  nextTick(() => {
    dataForm.value.clearValidate();
  });
  action.dialogFormVisible = false;
};
defineExpose({
  openDialog,
});
</script>

<template>
  <div>
    <el-dialog
      top="8vh"
      class="xm-dialog"
      v-model="action.dialogTableVisible"
      width="60%"
      title="标签管理"
    >
      <el-button class="xm-mb10 xm-mrAuto" type="primary" @click="openForm()">新增</el-button>
      <el-table height="400px" class="xm-TableCenter" :data="action.list" border>
        <el-table-column prop="name" label="标签名" />
        <el-table-column prop="animate_resource" label="标签图片">
          <template #default="scope">
            <xmImage
              :src="scope.row.animate_resource"
              :list="[scope.row.animate_resource]"
            />
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="new_animate_resource" label="标签svga">
          <template #default="scope">
            {{
              scope.row.new_animate_resource
                ? oss_url + scope.row.new_animate_resource
                : ""
            }}
          </template>
        </el-table-column>
        <el-table-column width="180" prop="created_time" label="创建时间" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" @click="openForm(scope.row)"
              >编辑
            </el-button>
            <el-button type="danger" @click="handleDelete(scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog
      top="8vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="60%"
      :title="action.title"
    >
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="标签名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请填写标签名称"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="标签图片" prop="animate_resource">
          <xmUpload v-model:file-url="form.animate_resource" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="标签svga">
          <xmUpload v-model:file-url="form.new_animate_resource" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
